package com.xb.loan.cashier.biz.job;

import com.xb.loan.cashier.biz.dao.bean.CashierChargeOrder;
import com.xb.loan.cashier.biz.service.CashierChargeOrderService;
import com.xb.loan.cashier.biz.task.TaskManagerFactory;
import com.xb.loan.cashier.biz.task.ThirdQueryChargeTask;
import com.xb.loan.cashier.biz.util.Configure;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 
 * 监控待处理充值订单定时任务
 */
public class ChargeQueryJob extends AbstractCronTask {
    
    private static final Logger log = LoggerFactory.getLogger(ChargeQueryJob.class);
    
    private CashierChargeOrderService cashierChargeOrderService;
    
    /**
     *1.分页查询N分钟前处理中的充值订单,如果没找到数据结束任务.
     *2.启动第三方支付单笔查询任务
     *
     */
    @Override
    public void run(){
        cashierChargeOrderService = getSpringBean("cashierChargeOrderService");
        log.info("run 开始查询处理中充值订单");
        int minute = Configure.getIntValue("job.chargequery.before.minute");
        int limit = Configure.getIntValue("job.chargequery.pagesize");//每次抓取的条数
        int start = 0;
        int pageNum = 0;
        while (true) {
            List<CashierChargeOrder> list = cashierChargeOrderService.queryByStatus(minute,limit,start);
            if (list==null || list.size()==0) {
                log.info("run 没找到处理中的订单强制退出!");
                break;
            }
            log.info("run 处理中总条数："+list.size());
            if (pageNum>0) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                   log.error(e.getMessage(),e);
                }
            }
            for (CashierChargeOrder cashierChargeOrder : list) {
                
                ThirdQueryChargeTask queryChargeTask = null;
                
                if (cashierChargeOrder.getPayChannelType()== ICashierEnumConst.CashierPayChannelType.ALIPAY.getType()) {
                    
                    //支付宝
                    queryChargeTask = getSpringBean("alipayWapQueryChargeTask");
                    
                }else if(cashierChargeOrder.getPayChannelType() == ICashierEnumConst.CashierPayChannelType.WECHATPAY.getType()){
                    
                    //微信
                    queryChargeTask = getSpringBean("weChatQueryChargeTask");
                    
                }else if(cashierChargeOrder.getPayChannelType() == ICashierEnumConst.CashierPayChannelType.UNIONPAY.getType()){
                    
                    //银联支付
                    queryChargeTask = getSpringBean("unionPayQueryChargeTask");
                }else if (cashierChargeOrder.getPayChannelType() == ICashierEnumConst.CashierPayChannelType.BANKCOMM.getType()) {
                	//交行支付
                    queryChargeTask = getSpringBean("bankcommQueryChargeTask");
				}
                
                //需要查询的充值订单
                queryChargeTask.setCashierChargeOrder(cashierChargeOrder);
                
                //把推送充值结果放到线程池中推送
                TaskManagerFactory.getTaskManager(ICashierEnumConst.TaskManagerCode.CHARGE_QUERY).addTask(queryChargeTask);
            }
            start = (++pageNum)*limit;
        }
    }
    
}
